package nuffzone;

import java.io.IOException;
import javax.servlet.http.*;
import nuffzone.activityManager;
import java.util.*;

import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;

@SuppressWarnings("serial")
public class activityServlet extends HttpServlet {
	@Override
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		
		
		UserService userService = UserServiceFactory.getUserService();
		com.google.appengine.api.users.User user = userService.getCurrentUser();
		
		activityManager AM = activityManager.getInstance();
		String result = "true";
		if(user==null){ result="userRequired"; }
		else{

		String activityName = req.getParameter("name");
		String activityDescr = req.getParameter("description");
		int activityLength = Integer.valueOf(req.getParameter("lengte")).intValue();
		String activityStarttime = req.getParameter("start");
		String activityDate = req.getParameter("date");		
		
		String startDay = activityDate.substring(0,2);
		int day = Integer.valueOf(startDay).intValue();
		String startMonth = activityDate.substring(3,5);
		int month = Integer.valueOf(startMonth).intValue();
		String startYear = activityDate.substring(6,10);
		int year = Integer.valueOf(startYear).intValue();
		String startHour = activityStarttime.substring(0,2);
		
		if(startHour.contains(":")){
			
			startHour = startHour.substring(0,1);
		}
		int uur = Integer.valueOf(startHour).intValue();
		String startMinutes = activityStarttime.substring(activityStarttime.length()-2);
		int minutes = Integer.valueOf(startMinutes).intValue();
		
		Calendar startDateCal = new GregorianCalendar();
		startDateCal.set(year, month-1, day, uur, minutes);
		Date startDate = startDateCal.getTime();
		
		Calendar stopDateCal = new GregorianCalendar();
		stopDateCal.set(year, month-1, day, uur, minutes+activityLength);
		Date stopDate = stopDateCal.getTime();
		
		ArrayList<Activity> activityList = AM.getAllUserActivities(user);
		for(Activity a:activityList)
		{
			Date AstartDate = new Date();
			Date AstopDate = new Date();
			if(a.getStartDate()!=null){AstartDate = a.getStartDate();}
			if(a.getStopDate()!=null){AstopDate = a.getStopDate();}
			
			int result1 = startDate.compareTo(AstartDate); //POS if startDate > AstartDate
			int result2 = startDate.compareTo(AstopDate);  //POS if startDate > AstopDate
			int result3 = stopDate.compareTo(AstartDate);  //POS if stopDate > AstartDate
			int result4 = stopDate.compareTo(AstopDate);   //POS if stopDate > AstopDate
			
			System.out.println(result1+ " "+result2 + " "+result3 + " "+result4);
			
			if(result1<0 && result3>0){
				System.out.println("false");
				result="false";}
			if(result2<0 && result4>0){
				System.out.println("false");
				result="false";}
		}

		
		int weekstr = startDateCal.get(Calendar.WEEK_OF_YEAR);
		String week = ""+weekstr;
		int daystr = startDateCal.get(Calendar.DAY_OF_MONTH);
		String dag = ""+daystr;
		
		//System.out.println(startDate.get(Calendar.DAY_OF_MONTH)+"-  "+startDate.get(Calendar.HOUR_OF_DAY)+":"+startDate.get(Calendar.MINUTE));
		//System.out.println(activityLength);
		//System.out.println(stopDate.get(Calendar.DAY_OF_MONTH)+"-  "+stopDate.get(Calendar.HOUR_OF_DAY)+":"+stopDate.get(Calendar.MINUTE));
		
		AM.createActivity(activityName,activityDescr,startDate,stopDate,"red",user, week,dag);
		
		
		}
		resp.sendRedirect("/home.jsp?activityCreation="+result);
	}
}